package com.sunny.common;

import java.io.Serializable;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResponse<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	private int status;
	private String msg;
	private T data;
	
	public ServerResponse(int status) {
		this.status = status;
	}
	
	public ServerResponse(int status, String msg) {
		this.status = status;
		this.msg = msg;
	}
	
	public ServerResponse(int status, T data) {
		this.status = status;
		this.data = data;
	} 
	
    public ServerResponse(int status, String msg, T data) {
		this.status = status;
		this.msg = msg;
		this.data = data;
	}

	public int getStatus() {
		return status;
	}

	public String getMsg() {
		return msg;
	}

	public T getData() {
		return data;
	}
	
	@JsonIgnore
	public boolean isSuccess() {
		return this.status == ServerResponseCode.SUCCESS.getCode();
	}
	
	public static <T> ServerResponse<T> createBySuccess() {
		return new ServerResponse<T>(ServerResponseCode.SUCCESS.getCode(), ServerResponseCode.SUCCESS.getMsg());
	}
	
	public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
		return new ServerResponse<T>(ServerResponseCode.SUCCESS.getCode(), msg);
	}
	
	public static <T> ServerResponse<T> createBySuccess(T data) {
		return new ServerResponse<T>(ServerResponseCode.SUCCESS.getCode(), data);
	}
	
	public static <T> ServerResponse<T> createBySuccessMessage(String msg, T data ) {
		return new ServerResponse<T>(ServerResponseCode.SUCCESS.getCode(), msg, data);
	}
	
	public static <T> ServerResponse<T> createByError() {
		return new ServerResponse<T>(ServerResponseCode.ERROR.getCode(), ServerResponseCode.ERROR.getMsg());
	}
	
	public static <T> ServerResponse<T> createByErrorMessage(String msg) {
		return new ServerResponse<T>(ServerResponseCode.ERROR.getCode(), msg);
	}
}
